# Copyright 1999-2011 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/sys-fs/vhba/vhba-20110915.ebuild,v 1.1 2011/11/23 09:40:00 tetromino Exp $

EAPI="4"

inherit eutils

DESCRIPTION="udev rules file for VHBA kernel module"
HOMEPAGE="http://cdemu.org"

LICENSE="GPL-2"
SLOT="0"
KEYWORDS="~amd64 ~x86"
IUSE=""

RDEPEND=">=sys-fs/udev-125"
DEPEND=""

S="${WORKDIR}"

pkg_setup() {
	enewgroup cdemu
}

src_install() {
	einfo "Generating udev rules ..."
	dodir /lib/udev/rules.d/
	cat > "${D}"/lib/udev/rules.d/70-vhba.rules <<-EOF || die
	# do not edit this file, it will be overwritten on update
	#
	KERNEL=="vhba_ctl", MODE="0660", OWNER="root", GROUP="cdemu"
	EOF
}

pkg_postinst() {
	# Older versions of vhba installed their rule file in /etc/udev/rules.d,
	# which overrides rules in /lib/udev/rules.d. We remove the old file
	# automatically if it is identical to the default one installed by
	# vhba-1.2.1 or 20101015-r1. Note that the comment at the top of the rules
	# file states that it can be automatically overwritten by the system.
	old_rules="${ROOT}etc/udev/rules.d/70-vhba.rules"
	if [[ -f "${old_rules}" ]]; then
		case "$(md5sum ${old_rules})" in
		  2959b3cf61cfe6e466cc3516a7bc19de* | 1e7a7e5d6d28c811eeec98ec26ed5d28* )
		  	elog
			elog "Removing old ${old_rules} ..."
			rm -f "${old_rules}" ||
				eerror "Failed, please remove ${old_rules} manually."
			;;
		  * )
			ewarn
			ewarn "The ${old_rules} file from a previous"
			ewarn "installation of ${PN} is overriding ${P}'s"
			ewarn "udev rules. Unless you had deliberately customized it,"
			ewarn "you should remove it."
			ewarn
			;;
		esac
	fi
}
